﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace JAAF.Builders
{
    /// <summary>
    /// An implementation of a <see cref="BuilderBase"/> that uses (slow) reflection
    /// for construction and property injection.
    /// </summary>
    public class ReflectionBuilder : BuilderBase
    {

        public ReflectionBuilder(BuilderBase _oFallbackBuilder = null)
            : base(_oFallbackBuilder)
        {

        }

        protected override object Construct(Type _oTypeToConstruct, params object[] _aoConstructorParams)
        {
            object oNewInstance = null;

            if (_aoConstructorParams == null)
                oNewInstance = _oTypeToConstruct.Assembly.CreateInstance(_oTypeToConstruct.FullName, false);
            else
                oNewInstance = _oTypeToConstruct.Assembly.CreateInstance(_oTypeToConstruct.FullName,
                                                                    false,
                                                                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                    null,
                                                                    _aoConstructorParams,
                                                                    null,
                                                                    null);

            return oNewInstance;
        }
    }
}
